package com.it2297.view;

import javax.swing.JPanel;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.MouseAdapter;

import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;

import com.it2297.component.AbstractPagination;
import com.it2297.renderer.BiddersCellRenderer;

@SuppressWarnings("serial")
public class BiddersBrowseView extends JPanel
{
	private final JScrollPane scrollPane = new JScrollPane();
	private final JTable table = new JTable();
	private final BiddersCellRenderer tableCellRenderer = new BiddersCellRenderer();
	private AbstractPagination pagination;
	private final DefaultTableModel tableModel = new DefaultTableModel(0, 3)
	{
		@Override
		public boolean isCellEditable(int row, int column)
		{
			return false;
		}
	};

	public BiddersBrowseView()
	{

		initGUI();
	}

	private void initGUI()
	{
		this.setBounds(100, 100, 750, 570);
		this.setLayout(new BorderLayout(0, 0));

		add(scrollPane, BorderLayout.CENTER);

		table.setRowHeight(100);
		table.setShowGrid(false);
		table.setModel(tableModel);
		table.setBackground(new Color(240, 240, 240));

		table.getColumnModel().getColumn(1).setMinWidth(100);
		table.getColumnModel().getColumn(1).setMaxWidth(100);
		table.getColumnModel().getColumn(2).setMinWidth(150);
		table.getColumnModel().getColumn(2).setMaxWidth(150);

		table.getColumnModel().getColumn(0).setHeaderValue("Item Info");
		table.getColumnModel().getColumn(1).setHeaderValue("No. of bids");
		table.getColumnModel().getColumn(2).setHeaderValue("Last bidded by");

		table.getColumnModel().getColumn(0).setCellRenderer(tableCellRenderer);
		table.getColumnModel().getColumn(1).setCellRenderer(tableCellRenderer);
		table.getColumnModel().getColumn(2).setCellRenderer(tableCellRenderer);

		scrollPane.setViewportView(table);
	}

	public void setPagination(AbstractPagination pagination)
	{
		if (this.pagination != null)
		{
			remove(this.pagination);
		}
		this.pagination = pagination;
		add(pagination, BorderLayout.SOUTH);
	}

	public void clearTableRow()
	{
		tableModel.setRowCount(0);
	}

	public void setPaginationCount(int paginationCount)
	{
		pagination.setCount(paginationCount);
	}

	public void setPaginationTotal(int paginationTotal)
	{
		pagination.setTotal(paginationTotal);
	}

	public void refresh()
	{
		pagination.refresh();
	}

	public void addTableRow(Object[] tableRowData)
	{
		tableModel.addRow(tableRowData);
	}

	public Object getSelectedItem()
	{
		return ((Object[]) table.getValueAt(table.getSelectedRow(), table.getSelectedColumn()))[0];
	}

	public void addTableMouseListener(MouseAdapter mouseListener)
	{
		table.addMouseListener(mouseListener);
	}
}
